package com.jcl.recruit.user.logic;

import com.jcl.core.common.page.PageResult;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.user.dao.CorpUserDao;
import com.jcl.recruit.user.model.User;
import com.jcl.recruit.user.vo.UserSearchEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserLogic

 * @Author yyliu
 * @Date 2018/9/14 9:19
 * @Version 1.0
 **/
@Component
public class UserLogic {

    @Autowired
    private CorpUserDao corpUserDao;

    public PageResult page(UserSearchEntity entity, Integer curPage, Integer pageSize) {

        return corpUserDao.page(entity, curPage, pageSize);
    }

    public Map<Integer, User> getUserMap(String corpCode, List<Integer> ids) {
        List<User> userList = corpUserDao.findUserListByIds(corpCode, ids);
        Map<Integer, User> userMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(userList)) {
            for (User user : userList) {
                userMap.put(user.getId(), user);
            }
        }
        return userMap;
    }

    public int countMembers(String corpCode) {
        Integer numbers = 0;

        Map<String, Object> map = corpUserDao.countMember(corpCode);
        if (null != map && null != map.get("members")) {
            numbers = Integer.parseInt(map.get("members").toString());
        }

        return numbers;
    }

    public Map<String, User> getAdminUserByCorpCode(List<String> corpCodes) {
        Map<String, User> userMap = new HashMap<>();
        List<User> userList = corpUserDao.findAdminUserListByCorp(corpCodes);
        if (CollectionUtils.isNotEmpty(userList)) {
            for (User user : userList) {
                userMap.put(user.getCorpCode(), user);
            }
        }
        return userMap;
    }

    public Map<String, User> getCorpCodeUserByUserId(List<Integer> userIds) {
        Map<String, User> userMap = new HashMap<>();
        List<User> userList = corpUserDao.findUserList(userIds);
        if (CollectionUtils.isNotEmpty(userList)) {
            for (User user : userList) {
                userMap.put(user.getCorpCode(), user);
            }
        }
        return userMap;
    }

    protected List<?> searchConditionList(UserSearchEntity entity) {
        if (UserSearchEntity.TYPE_CERTIFY.equals(entity.getType())) {
            entity.setStatus(Constants.UserInfo.USER_STATUS_UNACTIVE);
            return corpUserDao.findListByEntity(entity);
        } else if (UserSearchEntity.TYPE_ALL.equals(entity.getType())) {
            return corpUserDao.findListByEntity(entity);
        }
        return null;
    }

    public List<Map<String, Object>> selectUsersByCondition(String realname,
                                                            Integer isHr,
                                                            String corpCode,
                                                            Integer departId) {
        List<Map<String, Object>> userVos = corpUserDao.selectUsersByCondition(realname, isHr, corpCode, departId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Map<String, Object> entity : userVos) {
            entity.put("createtime", entity.get("createtime"));
        }
        return userVos;
    }

    public List<User> findListByEntity(UserSearchEntity entity) {
        return corpUserDao.findListByEntity(entity);
    }

    public List<User> findAll(String corpCode) {
        return corpUserDao.findAll(corpCode);
    }

    public List<User> findListCertification(String corpCode) {
        return corpUserDao.findListByStatus(corpCode, Constants.UserInfo.USER_STATUS_UNACTIVE);
    }

    public List<User> findUserListByIds(String corpCode, List<Integer> ids) {
        return corpUserDao.findUserListByIds(corpCode, ids);
    }

    public List<User> findUserList(List<Integer> ids) {
        return corpUserDao.findUserList(ids);
    }

    public List<User> findListByStatus(String corpCode, Integer status) {
        return corpUserDao.findListByStatus(corpCode, status);
    }

    public List<User> findAdminUserListByCorp(List<String> corpCodes) {
        return corpUserDao.findAdminUserListByCorp(corpCodes);
    }

    public Map<String, Object> selectPrimaryKey(String username, String password) {

        return corpUserDao.selectPrimaryKey(username, password);
    }

    public Map<String, Object> selectMaxIdKey() {
        return corpUserDao.selectMaxIdKey();
    }

    public Map<String, Object> countMember(String corpCode) {
        return corpUserDao.countMember(corpCode);
    }

    public Map<String, Object> selectPrimaryKeyByToken(String token) {
        return corpUserDao.selectPrimaryKeyByToken(token);
    }

    public Map<String, Object> selectPrimaryKeyById(Integer id) {
        return corpUserDao.selectPrimaryKeyById(id);
    }

    public Map<String, Object> selectPrimayByUsername(String username) {
        return corpUserDao.selectPrimayByUsername(username);
    }

    public User findOneById(Integer id) {
        return corpUserDao.findOneById(id);
    }

    public User findOne(String corpCode, Integer id) {
        return corpUserDao.findOne(corpCode, id);
    }

    public User findAdminUserByCorp(String corpCode) {
        return corpUserDao.findAdminUserByCorp(corpCode);
    }

    public int insertSelective(User record) {
        return corpUserDao.insertSelective(record);
    }

    public int updateByPrimaryKeySelective(User record) {
        return corpUserDao.updateByPrimaryKeySelective(record);
    }

    public int updateUserHead(Integer id, String photoId) {
        return corpUserDao.updateUserHead(id, photoId);
    }

    public Integer selectPrimaryIdByEmail(String email) {
        return corpUserDao.selectPrimaryIdByEmail(email);
    }
}
